{% import 'Dockerfile/docker.jinja2'    as docker %}
{% import 'Dockerfile/provision.jinja2' as provision %}
{% import 'Dockerfile/services.jinja2'  as services %}

{% macro official(role='', version='') -%}
    # Install php environment
    && apt-install \
        # Install tools
        imagemagick \
        graphicsmagick \
        ghostscript \
        jpegoptim \
        libjpeg-turbo-progs \
        pngcrush \
        optipng \
        apngopt \
        pngnq \
        pngquant \
        # Libraries
        libvips42 \
        librabbitmq4 \
        libldap-2.4-2 \
        libxslt1.1 \
        zlibc \
        zlib1g \
        libpq5 \
        libpng16-16 \
        libmcrypt4 \
        libzip4 \
        # Dev and headers
        libjpeg62-turbo-dev \
        libwebp-dev \
        libfreetype6-dev \
        libbz2-dev \
        libicu-dev \
        libldap2-dev \
        libldb-dev \
        libmcrypt-dev \
        libxml2-dev \
        libxslt1-dev \
        zlib1g-dev \
        libmemcached-dev \
        libpng-dev \
        libpq-dev \
        libzip-dev \
        libc-client-dev \
        libc-client2007e-dev \
        libkrb5-dev \
        libvips-dev \
        librabbitmq-dev \
        libmagick++-dev \
    # Install guetzli
    && wget https://github.com/google/guetzli/archive/master.zip \
    && unzip master.zip \
    && make -C guetzli-master \
    && cp guetzli-master/bin/Release/guetzli /usr/local/bin/ \
    && rm -rf master.zip guetzli-master \
    # Install new version of ICU
    && curl -sS -o /tmp/icu.tar.gz -L https://github.com/unicode-org/icu/releases/download/release-66-1/icu4c-66_1-src.tgz \
    && tar -zxf /tmp/icu.tar.gz -C /tmp && cd /tmp/icu/source && ./configure --prefix=/usr/local && make && make install && cd / && rm -rf /tmp/icu* \
    # Install extensions
{%- if version != '7.4' and version != '8.0' %}
    && docker-php-ext-configure intl --with-icu-dir=/usr/local \
    && docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ --with-webp-dir=/usr/include/ \
{%- else %}
    && PKG_CONFIG_PATH=/usr/local docker-php-ext-configure intl \
    && docker-php-ext-configure gd --with-jpeg --with-freetype --with-webp \
{%- endif %}
{%- if version == '8.0' %}
    && git clone --branch master --depth 1 https://github.com/php-amqp/php-amqp.git /usr/src/php/ext/amqp \
    && cd /usr/src/php/ext/amqp && git submodule update --init \
    && git clone --branch master --depth 1 https://github.com/Imagick/imagick.git /usr/src/php/ext/imagick \
{%- endif %}
    && docker-php-ext-configure ldap --with-libdir=lib/x86_64-linux-gnu/ \
    && PHP_OPENSSL=yes docker-php-ext-configure imap --with-kerberos --with-imap-ssl \
    && docker-php-ext-install \
        bcmath \
{%- if version == '8.0' %}
        amqp \
        imagick \
{%- endif %}        
        bz2 \
        calendar \
        exif \
        intl \
        gettext \
        ldap \
        mysqli \
{%- if version|int == 5 or version == '7.0' or version == '7.1' %}
        mcrypt \
{%- endif %}
        imap \
{%- if version != '7.4' and version != '8.0' %}
        hash \
{%- endif %}
        pcntl \
        pdo_mysql \
        pdo_pgsql \
        pgsql \
        soap \
        sockets \
        tokenizer \
        sysvmsg \
        sysvsem \
        sysvshm \
        shmop \
{%- if version != '8.0' %}
        xmlrpc \
{%- endif %}
        xsl \
        zip \
        gd \
        gettext \
        opcache \
{%- if version|int == 5 %}
    # Install extensions for PHP 5.x
    && pecl install apcu-4.0.11 \
    && printf "no --disable-memcached-sasl\n" | pecl install memcached-2.2.0 \
    && pecl install redis-4.3.0 \
    && pecl install mongodb \
    && pecl install imagick \
    && echo extension=apcu.so > /usr/local/etc/php/conf.d/apcu.ini \
    && echo extension=redis.so > /usr/local/etc/php/conf.d/redis.ini \
    && echo extension=imagick.so > /usr/local/etc/php/conf.d/imagick.ini \
    && echo extension=mongodb.so > /usr/local/etc/php/conf.d/mongodb.ini \
    && echo extension=memcached.so > /usr/local/etc/php/conf.d/memcached.ini \
{%- elif version == '8.0' %}
    && pecl install memcached \
    && pecl install apcu \
    && pecl install vips \
    && pecl install redis \
    && pecl install mongodb \
{%- else %}
    # Install extensions for PHP 7.x
    && pecl install apcu \
    && printf "no --disable-memcached-sasl\n" | pecl install memcached \
    && echo extension=memcached.so > /usr/local/etc/php/conf.d/memcached.ini \
    # Install vips (only works with PHP >= 7.0)
    && printf "\n" | pecl install vips \
    && echo extension=vips.so > /usr/local/etc/php/conf.d/vips.ini \
    && pecl install redis \
    && pecl install mongodb \
    && pecl install imagick \
    && pecl install amqp \
    && echo extension=apcu.so > /usr/local/etc/php/conf.d/apcu.ini \
    && echo extension=redis.so > /usr/local/etc/php/conf.d/redis.ini \
    && echo extension=imagick.so > /usr/local/etc/php/conf.d/imagick.ini \
    && echo extension=mongodb.so > /usr/local/etc/php/conf.d/mongodb.ini \
    && echo extension=amqp.so > /usr/local/etc/php/conf.d/amqp.ini \
{%- endif %}
    # Uninstall dev and header packages
    && apt-get purge -y -f --force-yes \
        libc-client-dev \
        libkrb5-dev \
        libbz2-dev \
        libicu-dev \
        libldap2-dev \
        libldb-dev \
        libmcrypt-dev \
        libxml2-dev \
        libxslt1-dev \
        zlib1g-dev \
        libpng-dev \
        libwebp-dev \
        libpq-dev \
        libzip-dev \
        libvips-dev \
        librabbitmq-dev \
        libmagick++-dev \
    && rm -f /usr/local/etc/php-fpm.d/zz-docker.conf \
    && curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin/ --filename=composer2 \
    && curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin/ --filename=composer1 --1 \
    && ln -sf /usr/local/bin/composer2 /usr/local/bin/composer \
    {{ services.php() }}
{%- endmacro %}

{% macro officialAlpine(role='', version='') -%}
    # Install php environment
    && apk-install \
        imagemagick \
        graphicsmagick \
        ghostscript \
        jpegoptim \
        pngcrush \
        optipng \
        pngquant \
        vips \
        rabbitmq-c \
        c-client \
        # Libraries
        libldap \
        icu-libs \
        libintl \
        libpq \
        libxslt \
        libzip \
        libmemcached \
        yaml \
        # Build dependencies
        autoconf \
        g++ \
        make \
        libtool \
        pcre-dev \
        gettext-dev \
        freetype-dev \
        libjpeg-turbo-dev \
        libpng-dev \
        vips-dev \
        krb5-dev \
        openssl-dev \
        imap-dev \
        imagemagick-dev \
        rabbitmq-c-dev \
        openldap-dev \
        icu-dev \
        postgresql-dev \
        libxml2-dev \
        ldb-dev \
        pcre-dev \
        libxslt-dev \
        libzip-dev \
        libmemcached-dev \
        yaml-dev \
    # Install guetzli
    && wget https://github.com/google/guetzli/archive/master.zip \
    && unzip master.zip \
    && make -C guetzli-master \
    && cp guetzli-master/bin/Release/guetzli /usr/local/bin/ \
    && rm -rf master.zip guetzli-master \
    # https://github.com/docker-library/php/issues/240
    && apk add gnu-libiconv --update-cache --repository http://dl-cdn.alpinelinux.org/alpine/edge/testing/ --allow-untrusted \
    # Install new version of ICU
    && curl -sS -o /tmp/icu.tar.gz -L https://github.com/unicode-org/icu/releases/download/release-66-1/icu4c-66_1-src.tgz \
    && tar -zxf /tmp/icu.tar.gz -C /tmp && cd /tmp/icu/source && ./configure --prefix=/usr/local && make && make install && cd / && rm -rf /tmp/icu* \
    # Install extensions
{%- if version == '7.4' or version == '8.0' %}
    && PKG_CONFIG_PATH=/usr/local docker-php-ext-configure intl \
    && docker-php-ext-configure gd --with-jpeg --with-freetype --with-webp \
{%- else %}
    && docker-php-ext-configure intl --with-icu-dir=/usr/local \
    && docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ --with-webp-dir=/usr/include/ \
{%- endif %}
    && docker-php-ext-configure ldap \
    && PHP_OPENSSL=yes docker-php-ext-configure imap --with-kerberos --with-imap-ssl \
    && docker-php-ext-install \
        bcmath \
        bz2 \
        calendar \
        exif \
{%- if version == '7.4' or version == '8.0' %}
        ffi \
{%- endif %}
        intl \
        gettext \
        ldap \
        mysqli \
        imap \
{%- if version != '7.4' and version != '8.0' %}
        hash \
{%- endif %}
        pcntl \
        pdo_mysql \
        pdo_pgsql \
        pgsql \
        soap \
        sockets \
        tokenizer \
        sysvmsg \
        sysvsem \
        sysvshm \
        shmop \
{%- if version != '8.0' %}
        xmlrpc \
{%- endif %}
        xsl \
        zip \
        gd \
        gettext \
        opcache \
    # Install extensions for PHP 7.x
    {%- if version == '7.3' or version == '7.4' or version == '8.0' %}
    # Memcached for 7.3 can currently only be built from master
    && MEMCACHED="`mktemp -d`" \
    && curl -skL https://github.com/php-memcached-dev/php-memcached/archive/master.tar.gz | tar zxf - --strip-components 1 -C $MEMCACHED \
    && docker-php-ext-configure $MEMCACHED \
    && docker-php-ext-install $MEMCACHED \
    && rm -rf $MEMCACHED \
    {%- else %}
    && printf "no --disable-memcached-sasl\n" | pecl install memcached-3.0.4 \
    && docker-php-ext-enable memcached \
    {%- endif %}
{%- if version == '8.0' %}
    && pecl install apcu \
    && pecl install vips \
    && pecl install yaml \
    && pecl install redis \
    && pecl install mongodb \
    #&& pecl install imagick \
    #&& pecl install amqp \
    && docker-php-ext-enable \
        apcu \
        vips \
        yaml \
        redis \
        #imagick \
        mongodb \
        #amqp \
{%- else %}
    # Install vips (only works with PHP >= 7.0)
    && pecl install apcu \
    && printf "\n" | pecl install vips \
    && pecl install redis \
    && pecl install mongodb \
    && pecl install imagick \
    && pecl install amqp \
    && pecl install yaml \
    && docker-php-ext-enable \
        apcu \
        redis \
        imagick \
        mongodb \
        amqp \
        vips \
        yaml \
{%- endif %}
    # Uninstall dev and header packages
    && apk del -f --purge \
        autoconf \
        g++ \
        make \
        libtool \
        pcre-dev \
        gettext-dev \
        freetype-dev \
        libjpeg-turbo-dev \
        libpng-dev \
        vips-dev \
        krb5-dev \
        openssl-dev \
        imap-dev \
        rabbitmq-c-dev \
        imagemagick-dev \
        openldap-dev \
        icu-dev \
        postgresql-dev \
        libxml2-dev \
        ldb-dev \
        pcre-dev \
        libxslt-dev \
        libzip-dev \
        libmemcached-dev \
        yaml-dev \
    && rm -f /usr/local/etc/php-fpm.d/zz-docker.conf \
    && curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin/ --filename=composer2 \
    && curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin/ --filename=composer1 --1 \
    && ln -sf /usr/local/bin/composer2 /usr/local/bin/composer \
    {{ services.php() }}
{%- endmacro %}

{% macro officialDevelopment(role='', version='') -%}
    # Install development environment
    && wget -q -O - https://packages.blackfire.io/gpg.key | apt-key add - \
    && echo "deb https://packages.blackfire.io/debian any main" | tee /etc/apt/sources.list.d/blackfire.list \
    && apt-install \
        blackfire-php \
        blackfire-agent \
    {%- if version|int == 5 %}
    && pecl install xdebug-2.5.5 \
    {%- elif version == '8.0' %}
    && pecl install xdebug \
    {%- elif version == '7.0' or version == '7.1' %}
    && pecl install xdebug-2.8.1 \
    {%- else %}
    && pecl install xdebug \
    {%- endif %}
    && docker-php-ext-enable xdebug \
    {{ services.phpDev() }}
{%- endmacro %}

{% macro officialDevelopmentAlpine(role='', version='') -%}
    # Install development environment
    {%- if version != '8.0' %}
    && wget -q -O blackfire-agent https://packages.blackfire.io/binaries/blackfire-agent/1.34.0/blackfire-agent-linux_static_amd64 \
    && mv blackfire-agent /usr/local/bin/ \
    && chmod +x /usr/local/bin/blackfire-agent \
    && wget -q -O blackfire.so https://packages.blackfire.io/binaries/blackfire-php/1.33.0/blackfire-php-alpine_amd64-php-{{ version|replace('.', '') }}.so \
    && mv blackfire.so "$(php -r "echo ini_get('extension_dir');")/blackfire.so" \
    && mkdir /var/run/blackfire/ \
    {%- endif %}
    && apk-install \
        make \
        autoconf \
        g++ \
    {%- if version|int == 5 %}
    && pecl install xdebug-2.5.5 \
    {%- elif version == '7.0' or version == '7.1' %}
    && pecl install xdebug-2.8.1 \
    {%- elif version == '8.0' %}
    && pecl install xdebug \
    {%- else %}
    && pecl install xdebug \
    {%- endif %}
    && apk del -f --purge \
        autoconf \
        g++ \
        make \
    && docker-php-ext-enable xdebug \
    {{ services.phpDev() }}
{%- endmacro %}
